<?php
/**
 * 
 * @author truonghm
 * @date 15/04/2011
 * @desc tao panel main
 */


class Ofx_Layout  {
	private $_layout;
	//private $_output;
	
	public function __construct($request){
		$this->_layout = new stdClass;	
		
		$vc_moduleName =$request->getModuleName();
    	$vc_controllerName =$request->getControllerName();
    	$vc_actionName =$request->getActionName();
    	
    	
    	$vc_node_id = $vc_moduleName."_".$vc_controllerName."_".$vc_actionName;
    	//echo $vc_node_id.":truo";
    	// khoi tao khung kieu viewport
    	$viewport = new PhpExt_Viewport();
		$viewport->setLayout(new PhpExt_Layout_BorderLayout());
		
		// Norht Region
		$north = new PhpExt_BoxComponent();
		$north->setApplyTo("north")
			  ->setHeight(40);			   

		//add nonrth to viewport	  
		$viewport->addItem($north,PhpExt_Layout_BorderLayoutData::createNorthRegion()
				 ->setMargins("0 0 0 5"));

		// West Region        
		$west = new PhpExt_Panel();
		$west->setTitle("West")
		     ->setCollapsible(true)     
		     ->setWidth(200)
		     ->setId("west")
		     ->setAutoScroll(true)
		     ->setHeader(false);
	    
		//tree loader
		$loader = new PhpExt_Tree_TreeLoader();
		$loader->setDataUrl("/menu/");
		
		//tree root
		$root = new PhpExt_Tree_AsyncTreeNode();
		$root->setText("OfficeX")
		     ->setDraggable(true)
		     ->setExpandable(true)
		     ->setId("treeFunction");
		     
		//create tree
		$tree = new PhpExt_Tree_TreePanel();
		$tree->setUseArrows(true)
		     ->setAnimate(true)
		     ->setEnableDd(true)
		     ->setContainerScroll(true)
		     ->setLoader($loader)
		     ->setBorder(false)
		     ->setHeight(300)
		     ->setSingleExpand(false)
		     ->setAutoScroll(false)
		     ->setId("treeFunction"); 
		
		//add toolbar to tree
		$tb = $tree->getTopToolbar();
		$tb->addTextItem(1,"Function");
		
		//add tree to west
		$west->addItem($tree); 
		
		//add west to viewport
		$viewport->addItem($west, 
		    PhpExt_Layout_BorderLayoutData::createWestRegion()
		        ->setSplit(true)
		        ->setMinSize(175)
		        ->setMaxSize(400)
		        ->setMargins("0 0 0 0"));

		        
		//info Onready tree
		$treeMenuExpandFunction = "function(){
    		treeFunction.root.cascade(function(n) {
     			root.firstChild.expand(true);     			
     			if(n.id=='$vc_node_id'){
     				//n.select(n.id);
     				n.getUI().addClass('tree-select');
     			}
    		})
    	}";
		
		
		//$this->_layout->output = array("out1"=>"out1","out2"=>"out2");
		$this->setOutput($root->getJavascript(false, "root"));		
		$this->setOutput($tree->getJavascript(false, "treeFunction"));
		$this->setOutput($tree->setRootNode($root));
		$this->setOutput($root->expand(true,false,PhpExt_Javascript::variable($treeMenuExpandFunction)));
		
		$this->_layout->viewport = $viewport;
		
	}
	
	/**
	 * add info to var output in layout
	 * @param $key key output
	 * @param $value value output
	 * 
	 */
	public function setOutput($value){
		$this->_layout->output[] = $value;
	}
	
	
	/**
	 * get info var output 
	 */
	public function getOutput(){
		$this->_layout->output;
	}
	
	/**
	 * get info var layout 
	 */
	public function getViewport(){
		return $this->_layout->viewport;
	}
	
	/**
	 * set info var viewport 
	 */
	public function setViewport($obj){
		$this->_layout->viewport->addItem($obj,PhpExt_Layout_BorderLayoutData::createCenterRegion());
	}
	
	/**
	 * get info var layout 
	 */
	public function getLayout(){
		return $this->_layout;
	}
	
}


?>